using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.OleDb; public partial class login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //以下产生随机的验证码,并在label1显示 Random ro = new Random(); if (!IsPostBack) { this.Label1.Text = ro.Next(1000, 9999).ToString(); } } protected void Button1_Click(object sender, EventArgs e) { if (this.name.Text != "")//判断用户名是否未空 { if (this.pwd.Text != "")//判断密码是否未空 { if (this.yanzhen1.Text != "")//判断验证码是否未空 { if (this.yanzhen1.Text == this.Label1.Text)//判断验证码是否相等 { string sql; sql = "select count(*) from userinfo where username='" + this.name.Text + "' and pwd='" + this.pwd.Text + "'";//建立sql查询语句 try { OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("./app_data/db.mdb"));//建立数据库连接 conn.Open(); OleDbCommand cmd = new OleDbCommand(sql, conn); int state =Convert.ToInt32( cmd.ExecuteScalar());//执行sql语句,并返回获得值 if (state == 0 || state > 1)//如果数据中没有记录或有多条记录则抱错 { this.Label2.Text = "用户不存在,请检测用户名和密码是否正确!"; } else { this.Label2.Text = "登入成功!" ; } conn.Close(); } catch (Exception a) { Response.Writea.Message); } } else { this.Label2.Text = "验证码不正确,请重新输入!"; } } else { this.Label2.Text = "验证码没有填写!"; } } else { this.Label2.Text = "密码没有填写!"; } } else { this.Label2.Text = "用户名没有填写!"; } } }